package com.zdpx.pxframework.auth.controller;

import com.zdpx.pxframework.auth.api.TokenControllerApi;
import com.zdpx.pxframework.auth.service.TokenService;
import com.zdpx.pxframework.core.constant.SecurityConstant;
import com.zdpx.pxframework.core.pojo.LoginUser;
import com.zdpx.pxframework.core.vo.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * TOKEN接口控制器
 *
 * @Description:
 * @Author: Cheng XiaoXiao
 * @Date: 2022/5/31 16:47
 */
@RestController
@RequestMapping("/token")
public class TokenController implements TokenControllerApi {
    @Resource
    private TokenService tokenService;

    /**
     * 根据token获取用户信息
     *
     * @param token token
     * @return 用户信息
     */
    @Override
    @GetMapping("/login-user")
    public Result<LoginUser> getLoginUserByToken(@RequestHeader(SecurityConstant.AUTHORIZATION) String token) {
        return Result.success(tokenService.getLoginUserByToken(token));
    }

    /**
     * 删除用户Token
     *
     * @param token token
     * @return 是否删除
     */
    @Override
    @DeleteMapping
    public Result<Boolean> deleteToken(@RequestHeader(SecurityConstant.AUTHORIZATION) String token) {
        tokenService.deleteToken(token);
        return Result.success(true);
    }

    /**
     * 刷新token的过期时间
     *
     * @param token token
     * @return 刷新结果信息
     */
    @Override
    @PutMapping
    public Result<Boolean> refreshToken(@RequestHeader(SecurityConstant.AUTHORIZATION) String token) {
        tokenService.refreshToken(token);
        return Result.success(true);
    }

}
